<script setup>
import { inject, onBeforeUnmount, onMounted, ref } from 'vue'
import Card from '@/views/new-screen/components/card.vue'
import DrawEcharts from '@/components/draw-echarts.vue'
import { getHelper } from '@/utils/map/index.js'

import test1 from '../../../../assets/imgs/img1@2x.png'
import bj from '../../../../assets/imgs/title_rycr.png'

let measure, _viewer, _layerManager

const mapId = inject('mapId')

defineProps({
  isExpanded: Boolean,
})

const areaWarnOptions = {
  backgroundColor: 'transparent',
  //你的代码
  tooltip: {
    trigger: 'axis',
    backgroundColor: 'rgba(13, 64, 71, 0.50)',
    borderColor: 'rgba(143, 225, 252, 0.60)',
    padding: 8,
    textStyle: {
      color: '#fff',
    },
  },
  legend: {
    show: false,
    data: ['危房报警', '边坡报警', '安防事件报警'],
    icon: 'rect',
    right: '3%',
    top: '5%',
    itemWidth: 12,
    itemHeight: 12,
    textStyle: {
      fontSize: 14,
      color: '#FFFFFF',
    },
  },
  grid: {
    left: '12%',
    right: '5%',
    bottom: '30',
    top: '10%',
  },
  xAxis: [
    {
      type: 'category',
      data: ['09:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00'],
      boundaryGap: false,
      axisPointer: {
        type: 'shadow',
      },
      axisLabel: {
        color: '#F5F5F5', //更改坐标轴文字颜色
        fontSize: 12, //更改坐标轴文字大小
      },
    },
  ],
  yAxis: [
    {
      type: 'value',
      axisLabel: {
        color: '#F5F5F5', //更改坐标轴文字颜色
        fontSize: 12, //更改坐标轴文字大小
      },
      splitLine: {
        show: true,
        lineStyle: {
          color: 'rgba(30, 48, 63, 1)',
          type: 'dashed',
        },
      },
    },

    // 折线部分
    {
      type: 'value',
      name: '',
      axisLabel: {
        show: false,
        color: '#F5F5F5', //更改坐标轴文字颜色
        fontSize: 12, //更改坐标轴文字大小
        formatter: '{value} %',
      },
      splitLine: {
        show: false,
      },
    },
  ],
  series: [
    {
      name: '报警次数',
      type: 'line',
      showSymbol: false,
      itemStyle: {
        shadowColor: '#fff',
        shadowBlur: 5,
        color: '#FF6059',
      },
      lineStyle: {
        width: 1,
        color: '#FF6059',
      },
      areaStyle: {
        width: 4,
        // opacity: 0.1,
        color: {
          type: 'linear',
          x: 0,
          y: 0,
          x2: 0,
          y2: 1,
          colorStops: [
            { offset: 0, color: 'rgba(255,96,89, 1)' },
            { offset: 1, color: 'rgba(255,96,89, 0.1)' },
          ],
          global: false,
        },
      },
      label: {
        show: false,
        position: 'top',
        fontSize: 12,
        color: '#F5F5F5',
        offset: [0, -10],
        formatter: '{c}', //添加单位
      },
      data: [490, 380, 200, 480, 480, 310, 370],
    },
  ],
}

const areaWarnList = ref([
  {
    name: 'A号楼',
    address: '办公楼大门',
    type: '人员闯入',
    humen: '(未识别出)',
    time: '2024-02-03 12:00:12',
    warnName: '#1专变房A',
    img: test1,
    warn: bj,
  },
  {
    name: 'A号楼',
    address: '办公楼大门',
    type: '人员闯入',
    humen: '(未识别出)',
    time: '2024-02-03 12:00:12',
    warnName: '#1专变房A',
    img: test1,
    warn: bj,
  },
  {
    name: 'A号楼',
    address: '办公楼大门',
    type: '人员闯入',
    humen: '(未识别出)',
    time: '2024-02-03 12:00:12',
    warnName: '#1专变房A',
    img: test1,
    warn: bj,
  },
  {
    name: 'A号楼',
    address: '办公楼大门',
    type: '人员闯入',
    humen: '(未识别出)',
    time: '2024-02-03 12:00:12',
    warnName: '#1专变房A',
    img: test1,
    warn: bj,
  },
])

let trajectoryList = ref([
  {
    address: '蔡家园区大门',
    time: '2024-01-22  12:22:23',
    isWarn: false,
    position: [106.50267889877305, 29.793468517068004],
  },
  {
    address: '蔡家园区大门左侧',
    time: '2024-01-22  12:22:23',
    isWarn: false,
    position: [106.50367889877315, 29.794468517068104],
  },
  {
    address: 'XX园区园区1号楼',
    time: '2024-01-22  12:22:23',
    isWarn: true,
    position: [106.50467889877325, 29.795468517068114],
  },
  {
    address: 'XX园区园区1号楼左侧',
    time: '2024-01-22  12:22:23',
    isWarn: false,
    position: [106.50567889877335, 29.796468517068124],
  },
  {
    address: 'XX园区园区1号楼左侧',
    time: '2024-01-22  12:22:23',
    isWarn: false,
    position: [106.50667889877345, 29.797468517068134],
  },
  {
    address: 'XX园区园区2号楼左侧',
    time: '2024-01-22  12:22:23',
    isWarn: false,
    position: [106.50767889877355, 29.798468517068144],
  },
  {
    address: 'XX园区园区2号楼左侧',
    time: '2024-01-22  12:22:23',
    isWarn: false,
    position: [106.50867889877365, 29.799468517068154],
  },
])

const entities = ref([])
const lineEntity = ref('')

let trajectoryActive = ref(null)

let showTrajectory = ref(false)

const showTrajectoryClick = () => {
  removeEntity()
  showTrajectory.value = true
  addLine()
  addPoint()
}

const trajectoryClose = () => {
  showTrajectory.value = false
  trajectoryActive.value = null
  removeEntity()
}

const trajectoryClick = (index) => {
  trajectoryActive.value = index
  if (entities.value.length) {
    entities.value.forEach((item) => {
      _viewer.entities.remove(item)
    })
  }
  entities.value = []
  addPoint()
}

const removeEntity = () => {
  if (lineEntity.value) {
    _viewer.entities.remove(lineEntity.value)
  }
  if (entities.value.length) {
    entities.value.forEach((item) => {
      _viewer.entities.remove(item)
    })
  }
  entities.value = []
  lineEntity.value = ''
}

const addLine = () => {
  let positions = []
  trajectoryList.value.map((item) => {
    positions.push(Number(item.position[0]))
    positions.push(Number(item.position[1]))
  })

  /// 创建线
  lineEntity.value = _viewer.entities.add({
    name: 'line',
    id: 'line',
    polyline: {
      positions: new Cesium.Cartesian3.fromDegreesArray(positions),
      width: 2,
      material: new Cesium.PolylineDashMaterialProperty({
        color: new Cesium.Color.fromCssColorString('#51BEFF'),
        dashLength: 10, //短划线长度
      }),
      clampToGround: true,
    },
    classificationType: Cesium.ClassificationType.S3M_TILE,
  })
  console.log(lineEntity.value)
}

const addPoint = () => {
  // const entities = []
  trajectoryList.value.forEach((item, index) => {
    if (index == trajectoryActive.value) {
      const entity = _viewer.entities.add({
        id: index,
        // name: '单体化标识面' + index,
        properties: new Cesium.PropertyBag(item),
        position: new Cesium.Cartesian3.fromDegrees(...item.position),
        billboard: {
          verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
          image: 'img/dwd_rw_sel.png',
          scale: 0.5,
          pixelOffset: new Cesium.Cartesian2(0, -20),
        },
        ellipse: {
          // 设置短半轴
          semiMinorAxis: 30.0,
          // 设置长半轴
          semiMajorAxis: 30.0,
          material: new Cesium.Color.fromCssColorString('#FFC152'),
          // height: 300.0,
          outline: false,
          outlineColor: Cesium.Color.WHITE,
          // 高度
          extrudedHeight: 0.0,
          // 设置椭圆旋转
          rotation: Math.PI / 4,
          // rotation: Cesium.Math.toRadians(45),
        },
        label: {
          text: (index + 1).toString(),
          font: 'Bold 40px MicroSoft YaHei',
          showBackground: false,
          scale: 0.5,
          fillColor: new Cesium.Color.fromCssColorString('#fff'), //设置文字填充的颜色
          outlineColor: new Cesium.Color.fromCssColorString('#fff'), //设置外框颜色
          outlineWidth: 0, //设置外框大小
          style: Cesium.LabelStyle.FILL_AND_OUTLINE, //填充方式，内外都填充
          verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
          // pixelOffset: new Cesium.Cartesian2(0, 12)
        },
      })
      entities.value.push(entity)
    } else {
      const entity = _viewer.entities.add({
        id: index,
        // name: '单体化标识面' + index,
        properties: new Cesium.PropertyBag(item),
        position: new Cesium.Cartesian3.fromDegrees(...item.position),
        billboard: {
          verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
          image: 'img/dwd_rw_nor.png',
          scale: 0.5,
          pixelOffset: new Cesium.Cartesian2(0, -20),
        },
        ellipse: {
          // 设置短半轴
          semiMinorAxis: 30.0,
          // 设置长半轴
          semiMajorAxis: 30.0,
          material: new Cesium.Color.fromCssColorString('#0B2446'),
          // height: 300.0,
          outline: false,
          outlineColor: Cesium.Color.WHITE,
          // 高度
          extrudedHeight: 0.0,
          // 设置椭圆旋转
          rotation: Math.PI / 4,
          // rotation: Cesium.Math.toRadians(45),
        },
        label: {
          text: (index + 1).toString(),
          font: 'Bold 40px MicroSoft YaHei',
          showBackground: false,
          scale: 0.5,
          fillColor: new Cesium.Color.fromCssColorString('#fff'), //设置文字填充的颜色
          outlineColor: new Cesium.Color.fromCssColorString('#fff'), //设置外框颜色
          outlineWidth: 0, //设置外框大小
          style: Cesium.LabelStyle.FILL_AND_OUTLINE, //填充方式，内外都填充
          verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
          // pixelOffset: new Cesium.Cartesian2(0, 12)
        },
      })
      entities.value.push(entity)
    }
  })
  console.log(entities.value)
}

onMounted(async () => {
  const { map, layerManager } = await getHelper(mapId)
  _viewer = map
  measure = new Cesium.Measure(map)
  _layerManager = layerManager
})
onBeforeUnmount(() => {
  showTrajectory.value = false
  removeEntity()
})
</script>
<template>
  <div :class="['right', isExpanded ? 'active' : '']">
    <div v-show="!showTrajectory">
      <card class="area-warn" name="区域报警记录">
        <template #title>
          <i class="title-icon"></i>
        </template>
        <div class="area-warn-title">
          <div class="name">今日报警次数</div>
          <div class="right-nums">
            <div class="right-num">0</div>
            <div class="right-num">6</div>
            <div class="right-num">4</div>
            <span>次</span>
          </div>
        </div>
        <div class="area-warn-chart">
          <draw-echarts
            :options="areaWarnOptions"
            height="220"
            width="400"
          ></draw-echarts>
        </div>
      </card>
      <card class="area-infos" name="园区信息">
        <template #title>
          <i class="title-icon"></i>
        </template>
        <div class="area-infos-list">
          <el-scrollbar>
            <div
              v-for="(item, index) in areaWarnList"
              :key="index"
              class="area-infos-list-item"
            >
              <div class="area-infos-list-item-top">
                <div class="area-infos-list-item-top-left">
                  <div class="area-infos-list-item-top-left-line title">
                    <span>房屋：</span>
                    <span>{{ item.name }}</span>
                  </div>
                  <div class="area-infos-list-item-top-left-line">
                    <span class="name">位置：</span>
                    <span class="value">{{ item.address }}</span>
                  </div>
                  <div class="area-infos-list-item-top-left-line">
                    <span class="name">报警类型：</span>
                    <span class="value" style="color: #ff6059">{{
                      item.type
                    }}</span>
                  </div>
                  <div class="area-infos-list-item-top-left-line">
                    <span class="name">人员姓名：</span>
                    <span class="value">{{ item.humen }}</span>
                  </div>
                  <div class="area-infos-list-item-top-left-line">
                    <span class="name">报警时间：</span>
                    <span class="value">{{ item.time }}</span>
                  </div>
                </div>
                <div
                  :style="{ backgroundImage: `url(${item.img})` }"
                  class="area-infos-list-item-top-img"
                >
                  <img
                    class="warn-img"
                    src="../../../../assets/imgs/ic_bj_area.png"
                  />
                  <img :src="item.warn" class="warn" />
                  <div class="warn-black"></div>
                  <div class="name">
                    <span class="point"></span>{{ item.warnName }}
                  </div>
                </div>
              </div>
              <div
                class="area-infos-list-item-bottom"
                @click="showTrajectoryClick"
              >
                <img class="icon" src="../../../../assets/imgs/ic_rygj.png" />
                <img
                  class="name"
                  src="../../../../assets/imgs/rygjck_img.png"
                />
              </div>
            </div>
          </el-scrollbar>
        </div>
      </card>
    </div>
    <div v-show="showTrajectory">
      <card class="visitor-trajectory-area-info" name="轨迹详情">
        <template #title>
          <i class="title-icon"></i>
        </template>

        <template #right>
          <div class="right-close" @click="trajectoryClose">
            <img src="../../../../assets/imgs/ic_gb.png" />
          </div>
        </template>
        <div class="trajectory-title">
          <div class="trajectory-title-info">
            <div class="trajectory-title-info-left">
              <span>徐某某</span>(183 6297 1313)
            </div>
            <div class="trajectory-title-info-right">XXXXXXXX公司</div>
          </div>
        </div>
        <div v-if="trajectoryList.length" class="trajectory-list">
          <el-scrollbar>
            <div
              v-for="(item, index) in trajectoryList"
              :key="index"
              :class="trajectoryActive == index ? 'active' : ''"
              class="trajectory-list-item"
              @click="trajectoryClick(index)"
            >
              <div class="trajectory-list-item-info">
                <div class="trajectory-list-item-info-left">
                  <span>{{ index + 1 }}</span>
                </div>
                <div class="trajectory-list-item-info-right">
                  <div class="trajectory-list-item-info-address">
                    {{ item.address
                    }}<img
                      v-if="item.isWarn"
                      src="../../../../assets/imgs/ic_bj.png"
                    />
                  </div>
                  <div class="trajectory-list-item-info-time">
                    {{ item.time }}
                  </div>
                </div>
              </div>
              <div class="trajectory-list-item-arrow">
                <img src="../../../../assets/imgs/gj_arrow.png" />
              </div>
            </div>
          </el-scrollbar>
        </div>
        <div v-else class="trajectory-list">
          <div class="trajectory-list-noData">
            <div class="trajectory-list-noData-img">
              <img src="../../../../assets/imgs/ic_zwgj.png" />
              <div>暂无轨迹</div>
            </div>
          </div>
        </div>
      </card>
    </div>
  </div>
</template>
<style lang="scss" scoped>
.right {
  .title-icon {
    display: inline-flex;
    background-repeat: no-repeat;
    background-size: 100% 100%;
    height: 61px;
    position: relative;
    bottom: 20px;
    // margin-top: 10px;
  }

  .right-close {
    height: 100%;
    position: absolute;
    right: 0;

    img {
      height: 16px;
      width: 16px;
      cursor: pointer;
    }
  }

  .area-warn {
    height: 346px;
    margin-bottom: var(--card-margin-bottom);

    .title-icon {
      width: 162px;
      background-image: url('@/assets/imgs/qybjjl_title.png');
    }

    &-title {
      height: 95px;
      background-repeat: no-repeat;
      background-size: 100% 100%;
      background-image: url('@/assets/imgs/img_wybj.png');
      padding-left: 113px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding-right: 30px;

      .name {
        font-family: YouSheBiaoTiHei;
        font-weight: 400;
        font-size: 18px;
        color: #ffffff;
        background: linear-gradient(0deg, #8dc4ff 0%, #ffffff 100%);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
      }

      .right-nums {
        display: flex;

        .right-num {
          width: 30px;
          height: 40px;
          background-repeat: no-repeat;
          background-size: 100% 100%;
          background-image: url('@/assets/imgs/img_sz_area.png');
          font-family: DINPro;
          font-weight: 500;
          font-size: 23px;
          color: #fff3e1;
          display: flex;
          justify-content: center;
          align-items: center;
          margin-right: 6px;
        }

        span {
          font-family: Source Han Sans SC;
          font-weight: 500;
          font-size: 12px;
          color: #c4eaff;
          margin-top: 18px;
        }
      }
    }
  }

  .area-infos {
    height: 598px;

    .title-icon {
      width: 122px;
      background-image: url('@/assets/imgs/yqxx_title.png');
    }

    &-list {
      height: 100%;
      overflow: hidden;

      &-item {
        height: 200px;
        background-repeat: no-repeat;
        background-size: 100% 100%;
        background-image: url('@/assets/imgs/card_d_area.png');
        padding-top: 15px;
        padding-left: 10px;
        padding-right: 11px;
        width: 100%;
        margin-top: 10px;

        &-top {
          display: flex;
          justify-content: space-between;
          width: 100%;
          height: 80%;

          &-left {
            flex: 1;

            &-line {
              font-family: Source Han Sans SC;
              font-weight: 400;
              font-size: 12px;
              color: #ffffff;
              height: 30px;
              line-height: 30px;
              padding-left: 10px;

              .name {
                color: #c4eaff;
              }

              .value {
              }
            }

            .title {
              height: 24px;
              line-height: 24px;
              padding-left: 10px;
              font-family: Source Han Sans SC;
              font-weight: bold;
              font-size: 14px;
              color: #ffffff;
              background-repeat: no-repeat;
              background-size: 100% 100%;
              background-image: url('@/assets/imgs/title_bt.png');
            }
          }

          &-img {
            width: 187px;
            height: 100%;
            background-repeat: no-repeat;
            background-size: 100% 100%;
            position: relative;

            .name {
              width: 100%;
              height: 24px;
              display: flex;
              align-items: center;
              background: rgba($color: #001d31, $alpha: 0.8);
              font-family: Source Han Sans SC;
              font-weight: 500;
              font-size: 12px;
              color: #ffffff;
              position: absolute;
              bottom: 0;

              .point {
                width: 7px;
                height: 7px;
                background: #21df95;
                border-radius: 50%;
                margin-left: 6px;
                margin-right: 6px;
              }
            }

            .warn-black {
              height: 100%;
              width: 100%;
              position: absolute;
              top: 0;
              left: 0;
              background: rgba($color: #000000, $alpha: 0.45);
            }

            .warn-img {
              height: 64px;
              width: 64px;
              position: absolute;
              top: 13px;
              left: calc(50% - 32px);
              z-index: 1000;
            }

            .warn {
              width: 114px;
              height: 26px;
              position: absolute;
              top: 71px;
              left: calc(50% - 57px);
              z-index: 1000;
            }
          }
        }

        &-bottom {
          height: 20%;
          width: 100%;
          display: flex;
          justify-content: center;
          align-items: center;
          cursor: pointer;

          .icon {
            height: 18px;
            width: 18px;
          }

          .name {
            width: 78px;
            height: 13px;
          }
        }
      }
    }
  }

  .visitor-trajectory-area-info {
    height: 966px;

    .title-icon {
      width: 122px;
      background-image: url('@/assets/imgs/gjxq_title.png');
    }

    .trajectory-title {
      height: 75px;
      margin-top: 10px;
      background-size: 100% 100%;
      background-repeat: no-repeat;
      background-image: url('@/assets/imgs/gj_label_title.png');
      padding-left: 83px;
      padding-right: 21px;
      margin-bottom: 10px;

      &-info {
        height: 100%;
        display: flex;
        justify-content: space-between;
        align-items: center;
        font-family: Source Han Sans SC;
        color: #c4eaff;

        &-left {
          font-size: 14px;
          font-weight: 400;

          span {
            font-weight: bold;
            font-size: 18px;
            color: #ffffff;
            background: linear-gradient(0deg, #8dc4ff 0%, #ffffff 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
          }
        }

        &-right {
          font-weight: 400;
          font-size: 12px;
        }
      }
    }

    .trajectory-list {
      height: calc(100% - 100px);
      // overflow: auto;
      margin-top: 20px;
      padding-left: 20px;

      &-item {
        cursor: pointer;
        margin-bottom: 10px;

        &-info {
          height: 68px;
          display: flex;
          align-items: center;

          &-left {
            width: 48px;
            height: 48px;
            line-height: 48px;
            text-align: center;
            background-size: 100% 100%;
            background-repeat: no-repeat;
            background-image: url('@/assets/imgs/sjz_d_nor.png');
            font-family: Source Han Sans SC;
            font-weight: bold;
            font-size: 16px;
            color: #ffffff;
          }

          &-right {
            height: 68px;
            font-family: Source Han Sans SC;
            font-weight: 400;
            font-size: 12px;
            color: #c4eaff;
            width: 370px;
            position: relative;
            left: -24px;
            padding-left: 24px;
            line-height: 16px;
            background-size: 100% 100%;
            background-repeat: no-repeat;
          }

          &-address {
            font-family: Source Han Sans SC;
            font-weight: bold;
            font-size: 18px;
            color: #ffffff;
            background: linear-gradient(0deg, #8dc4ff 0%, #ffffff 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            // line-height: 30px;
            height: 30px;
            margin-top: 8px;
            display: flex;
            align-items: center;

            img {
              width: 24px;
              height: 24px;
              margin-left: 4px;
            }
          }
        }

        &-arrow {
          padding-left: 10px;

          img {
            height: 28px;
            width: 28px;
          }
        }

        &:last-child {
          .trajectory-list-item-arrow {
            display: none;
          }
        }

        &:hover {
          .trajectory-list-item-info-left {
            background-image: url('@/assets/imgs/sjz_d_sel.png');
          }

          .trajectory-list-item-info-right {
            background-image: url('@/assets/imgs/gj_select.png');
          }
        }

        &.active {
          .trajectory-list-item-info-left {
            background-image: url('@/assets/imgs/sjz_d_sel.png');
          }

          .trajectory-list-item-info-right {
            background-image: url('@/assets/imgs/gj_select.png');
          }
        }
      }

      &-noData {
        height: 100%;
        width: 100%;
        display: flex;
        justify-content: center;
        align-items: center;

        &-img {
          img {
            width: 48px;
            height: 48px;
          }

          div {
            margin-top: 10px;
            font-family: Source Han Sans SC;
            font-weight: 400;
            font-size: 14px;
            color: #c4eaff;
          }
        }
      }
    }
  }
}
</style>
